﻿
$(function () {
    //初始化列表信息
    init();
});

//初始化列表信息
function init(queryData) {
    $('#list').datagrid({
        title: '考勤配置表-主表',
        idField: 'SignConfigID',
        fitColumns: 'true',
        autoRowHeight: true,
        singleSelect: true,
        nowrap: true,
        striped: true,
        pagination: true,
        url: 'SignConfig/GetList',
        columns: [[
			{ field: 'ck', checkbox: true },
			{ field: 'SignConfigID', title: '考勤配置表ID', hidden: true },
			{ field: 'SignName', title: '考勤名称', width: 180 },
			{
			    field: 'WeekDays', title: '工作日', width: 180,
			    formatter: function (value, row, index) {
			        return value.replace('1', '周一').replace('2','周二').replace('3',"周三").replace('4','周四').replace('5','周五').replace('6','周六').replace('7','周日');
			    }
			},//记录周几工作；例如 ： 1,2,3,4,5 表示周一到周五工作； 
            { field: 'WorkTime', title: '上班时间', width: 180 },
			{ field: 'OpenOffset', title: '上班打卡偏移时间', width: 180 },//上班打卡时间有个范围 8点上班，7点~9点之间可以打卡；偏移量就是1个小时;单位为分钟
			{ field: 'CloseOffset', title: '下班打卡偏移时间', width: 180 },
			{ field: 'Remark', title: '备注', width: 180 },

        ]],
    });
}

//弹出增加窗口
function add() {
    $('#dlg').dialog('open').dialog('setTitle', '增加');
    //$('#fm').form('clear');
    ClearInput();
    ClearTableRow();
    $('#div_detail').hide();
    url = '/SignConfig/Create';
}

//弹出修改窗口
function edit() {
    var row = $('#list').datagrid('getSelected');
    if (row) {
        $('#dlg').dialog('open').dialog('setTitle', '修改');
        $('#fm').form('load', row);
        url = '/SignConfig/Edit';

        SetWeekDays(row.WeekDays);
        //绑定明细信息
        $.post('/SignConfig/GetConfigDetailList', { signConfigID: row.SignConfigID },
                    function (result) {
                        SetFormInputItemValue(result);
                    });
    }
}

function GetWeekDays() {
    var f_str = '';

    $("input[name='week']").each(function () {

        if ($(this).attr("checked") == "checked") {
            
            f_str += $(this).attr("value") + ",";
        }
    });
    if (f_str != "") {
        f_str = f_str.substr(0, f_str.length - 1);
    };
    $('#WeekDays').val(f_str);
}

function SetWeekDays(weekDays) {
    var weeks = new Array();
    weeks = weekDays.split(",");
    for (i = 0; i < weeks.length; i++) {
        var week = weeks[i];

        var weekid = "week" + week;
        $('#' + weekid).attr("checked", true);
    }
}

//设置页面输入项值
function SetFormInputItemValue(strJsonInputItemValue) {
    if (strJsonInputItemValue != '[]') {
        var objJsonArray = JSON.parse(strJsonInputItemValue);
        var rows = objJsonArray.length / 2;
        ClearTableRow();
        if (rows > 0) {
            for (var i = 0; i < rows ; i++) {
                InsertTable("", "", i);
            }
            $('#div_detail').show();
        }

        for (var i = 0; i < objJsonArray.length; i++) {
            var htmlControl = document.getElementById(objJsonArray[i].name);

            if (htmlControl != null) {
                htmlControl.value = objJsonArray[i].value;
            }
        }
        
    }
}

function ClearTableRow() {
    var rows = document.all.yltable.rows.length;
    for (i = 1; i < rows; i++) {
        document.all.yltable.deleteRow(1);
    }
}

//保存数据
function save() {
    GetWeekDays();//获取工作日
    GetFormInputItemValue();//获取明细信息
    $('#fm').form('submit', {
        url: url,
        onSubmit: function () {
            return $(this).form('validate');
        },
        success: function (result) {
            if (result == 'OK') {
                $('#dlg').dialog('close');      // close the dialog  
                $('#list').datagrid('reload');    // reload the user data  
            } else {
                $.messager.show({
                    title: 'Error',
                    msg: result
                });
            }
        }
    });
}



//删除
function del() {
    var rows = $('#list').datagrid('getSelected');
    if (rows) {
        $.messager.confirm(delDlgTitle, delDlgMsg, function (r) {
            if (r) {
                $.post('/SignConfig/Delete', { id: rows.SignConfigID },
                    function (result) {
                        if (result == 'OK') {
                            $("#list").datagrid('clearSelections');
                            $('#list').datagrid('reload');    // reload the user data  
                        } else {
                            $.messager.show({   // show error message  
                                title: 'Error',
                                msg: result
                            });
                        }
                    });
            }
        });
    }
}


//清空页面录入项
function ClearInput() {
    $('input:text').val("");
    $('textarea').val("");
    $('input:checkbox').attr("checked",false);
}

//设置只读页面
function SetReadonly() {
    //将input元素设置为readonly
    $('input:text').attr("readonly", "readonly");
    $('textarea').attr("readonly", "readonly");
    $('select').attr("disabled", "true");
    $('input:checkbox').attr("disabled", true);
}
